package com.tyqx.hashchainbackend.service.impl;

import com.tyqx.hashchainbackend.service.SysRoleResourceService;
import com.tyqx.hashchainbean.module.SysRoleResource;
import com.tyqx.hashchainbean.vo.*;
import com.tyqx.hashchaindao.mapper.SysRoleResourceMapper;
import hashchainutil.common.Constants;
import hashchainutil.common.EmptyUtils;
import hashchainutil.common.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("sysRoleResourceService")
public class SysRoleResourceServiceImpl implements SysRoleResourceService {

    @Resource
    private SysRoleResourceMapper sysRoleResourceMapper;

    public SysRoleResource getById(Long rrId) throws Exception {
        return sysRoleResourceMapper.getById(rrId);
    }

    @Override
    public List<PerOrResourceParamVO> queryRoleResource(Long roleId) throws Exception {
        return sysRoleResourceMapper.getListById(roleId);
    }

    public Integer getCountByMap(Map<String, Object> param) throws Exception {
        return sysRoleResourceMapper.getCountByMap(param);
    }

    @Override
    public Integer save(List<SysRoleResourceVO> sysRoleResourceList) throws Exception {
        return sysRoleResourceMapper.save(sysRoleResourceList);
    }


    public Integer modify(SysRoleResource sysRoleResource) throws Exception {
        return sysRoleResourceMapper.modify(sysRoleResource);
    }

    public Integer removeById(Long rrId) throws Exception {
        return sysRoleResourceMapper.removeById(rrId);
    }

    public Page<List<SysRoleResource>> queryPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = sysRoleResourceMapper.getCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SysRoleResource> sysRoleResourceList = sysRoleResourceMapper.getListByMap(param);
        page.setRows(sysRoleResourceList);
        return page;
    }

    @Override
    public boolean removeById(Long roleId, List<Long> idMap) throws Exception {
        for (Long id : idMap) {
            sysRoleResourceMapper.removeByRoleAndRrId(roleId, id);
        }
        return true;
    }

    @Override
    public Integer removeByRoleId(Long roleId) {
        return sysRoleResourceMapper.removeByRoleId(roleId);
    }

    @Override
    public Integer removeByResId(Long resId) {
        return sysRoleResourceMapper.removeByResId(resId);
    }

    @Override
    public List<SysResourceIDVO> getSysRoleResourceByRoleId(Long roleId) {
        return sysRoleResourceMapper.getSysRoleResourceByRoleId(roleId);
    }

}
